from datetime import datetime

from flask_login import UserMixin

from exts import db


class UserModel(UserMixin, db.Model):
    __tablename__ = "user"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # 主键
    password = db.Column(db.String(200), nullable=False)  # 密码
    email = db.Column(db.String(50), nullable=False, unique=True)  # 邮箱
    username = db.Column(db.String(50), default=email, nullable=False)  # 用户名
    state = db.Column(db.Boolean, default=False)  # 在线状态
    is_delete = db.Column(db.Boolean, default=False)  # 启用状态
    join_time = db.Column(db.DateTime, default=datetime.now)  # 注册时间


class TopicModel(db.Model):
    __tablename__ = "topic_content"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    title = db.Column(db.Text, nullable=False)
    type = db.Column(db.String(200), nullable=False)
    options = db.Column(db.JSON, nullable=False)
    content = db.Column(db.Text, nullable=False)
    difficulty = db.Column(db.String(30), nullable=False)
    answer = db.Column(db.Text, nullable=False)
    explanation = db.Column(db.Text, nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.now)

    def to_dict(self):
        return {
            'id': self.id,
            'user_id': self.user_id,
            'type': self.type,
            'title': self.title,
            'content': self.content,
            'answer': self.answer,
            'difficulty': self.difficulty,
            'explanation': self.explanation,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S')
        }
